package com.zhw.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.extern.slf4j.Slf4j;

/**
 * @author weizhang52
 * @description:  channel注册后，会执行里面相应的初始化方法
 * @date 2019/4/16
 * @modify
 */
@Slf4j
public class HelloNettyInit extends ChannelInitializer<SocketChannel> {

    /**
     * 每个channel由多个handler组成管道
     * @param socketChannel
     * @throws Exception
     */
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        // 通过SocketChannel获取相应的管道
        ChannelPipeline channelPipeline = socketChannel.pipeline();

        // 给管道添加handler
        // HttpServerCodec自带的拦截器，当请求到服务端，先解码然后相应回去编码
//        channelPipeline.addLast("HttpServerCodec",new HttpServerCodec());
        channelPipeline.addLast(new StringDecoder());
        channelPipeline.addLast(new StringEncoder());
        // 添加自定义handler，返回Hello
//        channelPipeline.addLast("myHandler",null);
        channelPipeline.addLast("myHandler",new MyHandler());

    }

}
